import {
  RoleAdd,
  getRoleList,
  getRoleInfoById,
  updateRoleInfoById,
  deleteRoleInfoById,
  getAuthList,
} from '@/network/api/userListApi';

let initState = () => ({
  // 列表信息
  roleList: [], //角色信息列表
  total: 0,
  pageSize: 6,
  pageNum: 1,
  roleName: '', //角色搜索信息

  roleAuthList: [], //全部权限列表
  checkedList: [], //选择的权限列表
  drawerVisible: false, //抽屉开关
  roleVisibledrawerType: 'add', //抽屉类型
  roleId: '', //userId

  // authCheckedList:[]  //权限选择列表
});

export default {
  namespace: 'Role',
  state: { ...initState() },
  effects: {
    //查询所有用户权限
    *getAuthList({ payload }, { call, put }) {
      return yield call(getAuthList);
    },

    // 添加角色
    *addRole({ payload }, { call, put }) {
      var obj = { body: payload };
      return yield call(RoleAdd, obj);
    },

    // 获取角色分页列表
    *getRoleList({ payload }, { call, put }) {
      var obj = { params: payload };
      return yield call(getRoleList, obj);
    },
    // 获取角色详情
    *getRoleInfoById({ payload }, { call, put }) {
      var obj = { params: payload };
      return yield call(getRoleInfoById, obj);
    },
    // 修改角色信息
    *updateRoleInfoById({ payload }, { call, put }) {
      var obj = { body: payload };
      return yield call(updateRoleInfoById, obj);
    },
    // 删除角色
    *deleteRoleInfoById({ payload }, { call, put }) {
      var obj = { params: payload };
      return yield call(deleteRoleInfoById, obj);
    },
  },

  reducers: {
    setState(state, action) {
      return { ...state, ...action.payload };
    },
    init(state, action) {
      return { ...initState(), ...action.payload };
    },
  },
};
